package io.noties.markwon.plugin.ins

import io.noties.markwon.base.AbstractMarkwonPlugin
import io.noties.markwon.base.MarkwonSpansFactory
import io.noties.markwon.base.MarkwonVisitor
import org.commonmark.ext.ins.Ins
import org.commonmark.ext.ins.internal.InsDelimiterProcessor

import org.commonmark.parser.Parser

class InsPlugin : AbstractMarkwonPlugin() {

    override fun configureParser(builder: Parser.Builder) {
        builder.customDelimiterProcessor(InsDelimiterProcessor())
    }

    override fun configureSpansFactory(builder: MarkwonSpansFactory.Builder) {
        builder.setFactory(Ins::class.java){configuration, props ->
            InsSpan(configuration.theme)
        }
    }

    override fun configureVisitor(builder: MarkwonVisitor.Builder) {
        builder.on(Ins::class.java){visitor, node ->
            val length = visitor.length()
            visitor.visitChildren(node)
            visitor.setSpansForNodeOptional(node, length)
        }
    }

    companion object{
        fun create(): InsPlugin = InsPlugin()
    }
}